﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;
using System.Data;

namespace SkWeb.FYCS
{
    public partial class TB_CLLTFEdit : System.Web.UI.Page
    {
        #region 事件处理程序

        //页面加载事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblMsg.Text = "";
                lblPath.Text = "费用预算 >> 车辆路途费";
                if (!IsPostBack)
                {
                    if (Request["action"] == "add")
                    {
                        btnEdit.Visible = false;
                        InitData(false);
                    }
                    if (Request["action"] == "update")
                    {
                        btnAdd.Visible = false;
                        InitData(true);
                    }
                }
            }
        }

        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                TB_CLLTFInfo model = new TB_CLLTFInfo();
                TB_CLLTFBLL BLL = new TB_CLLTFBLL();
                int IDD = 0;
                DataTable dt = BLL.getMaxID().Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (!string.IsNullOrEmpty(dt.Rows[0]["maxid"].ToString()))
                    {
                        IDD = int.Parse(dt.Rows[0]["maxid"].ToString());
                    }
                }

                model.ID = IDD + 1;
                model.CLLT = txtCLLT.Text.Trim();
                if (!txtCLLTF.Text.Trim().Equals(string.Empty)) model.CLLTF = Convert.ToDecimal(txtCLLTF.Text.Trim());
                bool isOK = BLL.Add(model);
                if (isOK)
                {
                    Response.Redirect("TB_CLLTFList.aspx");
                }
                else
                {
                    lblMsg.Text = "保存失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息:" + ex.Message;
            }
            #endregion
        }

        //修改
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            #region
            try
            {
                int ID = Convert.ToInt32(Request["id"].ToString());
                TB_CLLTFInfo model = new TB_CLLTFInfo();
                TB_CLLTFBLL BLL = new TB_CLLTFBLL();
                model = BLL.GetModel(ID);
                model.ID = Convert.ToInt32(ID);
                model.CLLT = txtCLLT.Text.Trim();
                if (!txtCLLTF.Text.Trim().Equals(string.Empty)) model.CLLTF = Convert.ToInt32(txtCLLTF.Text.Trim());
                bool isOK = BLL.Update(model);
                if (isOK)
                {
                    Response.Redirect("TB_CLLTFList.aspx");
                }
                else
                {
                    lblMsg.Text = "保存失败！";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息:" + ex.Message;
            }
            #endregion

        }

        #endregion 事件处理程序


        #region 函数集

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="isEdit">是否是编辑</param>
        private void InitData(bool isEdit)
        {
            #region

            //初始化基本信息
            if (isEdit)
            {
                int ID = Convert.ToInt32(Request["id"].ToString());
                TB_CLLTFInfo model = new TB_CLLTFInfo();
                TB_CLLTFBLL BLL = new TB_CLLTFBLL();
                model = BLL.GetModel(ID);
                hidId.Value = ID.ToString();
                txtCLLT.Text = model.CLLT.ToString();
                txtCLLTF.Text = model.CLLTF == int.MinValue ? string.Empty : model.CLLTF.ToString();

            }

            #endregion
        }

        #endregion 函数集
    }
}